﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class MVTCREDITReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        #endregion
        public MVTCREDITReport()
            : base()
        {
        }
        public MVTCREDITReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            #region Excel  columns
            
            #endregion           						
            /*EKPNEUD	VRPRENR	VRKKOMPL	VVTLIEFSCH	VKPNEUBEN	PI	VKPAUFPOS	
             * KDKUNDNA	VKPWAEHRSL	VKPART	VKPFARBE	ORDQTY	VKPMEEH	ORDMTR	SOPRICE	
             * SOMTRPRICE	POPRICE	POMTRPRICE	RATE	QDMTRPRICE	DELQTY	POSUR	SOSUR COMMISSION,D.VEVERTNA	PROFITS
*/
        }
        #region------------------------------Function------------------------

        /*public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                
                Decimal I = 0;
                DataTable dtSoruce = setSoruce.Tables["EKVKVR"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());                        
                        y++;
                    }
                    I = System.Convert.ToDecimal(dataRow["PROFITS"].ToString().Trim());
                    
                    if (I <0)
                    {
                        exAgent.WorkSheet.Rows[x].Font.Color = 999900;
                    }                                       
                    x++;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["CREDIT"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("CODE", typeof(System.String));
                dtOutput.Columns.Add("CUSTOMER", typeof(System.String));
                dtOutput.Columns.Add("CURRENCY", typeof(System.String));
                dtOutput.Columns.Add("CREDIT", typeof(System.String));
                dtOutput.Columns.Add("ORDERVALUE", typeof(System.String));
                dtOutput.Columns.Add("ADEPOSIT", typeof(System.String));
                dtOutput.Columns.Add("GOODSVALUE", typeof(System.String));
                dtOutput.Columns.Add("ABEFORSHIP", typeof(System.String));
                dtOutput.Columns.Add("AVRVALUES", typeof(System.String));
                dtOutput.Columns.Add("REMAINVALUE", typeof(System.String));
                dtOutput.Columns.Add("REMAINCREDIT", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                /*CODE	CUSTOMER	CURRENCY	CREDIT	ORDERVALUE	ADEPOSIT	GOODSVALUE	ABEFORSHIP	
                    AVRVALUES	REMAINVALUE REMAINCREDIT*/
                exAgent.DataTableToExcel(dtOutput, 2, 1);

                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 4, count + 1, 11);
                exAgent.SetCellValue(1, 1, "客户编号");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "MVTCREDIT";
            strExcelFile = @"MVT\MVTStandard.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            //exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"客户信用" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(dsSource, 2, 1);
            base.WriteExcelReport("OpenExcel");
            
        }
        #endregion
    }
}